package com.yhhl.common;

import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Deprecated
public class ZkClientConfigWatcher {

	private final static Logger logger = LogManager.getLogger(ZkClientConfigWatcher.class);

	private ZkClient zkClient;

	private ConfigListener configListener;

	private IZkClientConfigManage configManage;

	public ZkClientConfigWatcher(ZkClient zkClient, IZkClientConfigManage configManage) {
		this.zkClient = zkClient;
		this.configManage = configManage;
		this.initConfig();
	}

	private void initConfig() {
		configListener = new ConfigListener();
	}

	public void watcher(String key) {
		zkClient.subscribeDataChanges(key, configListener);
		zkClient.subscribeChildChanges(key, configListener);
	}

	/**
	 * 配置监听器
	 * 
	 * @author flyking
	 * 
	 */
	private class ConfigListener implements IZkDataListener, IZkChildListener {
		public void handleDataChange(String dataPath, Object data) throws Exception {
			logger.info("触发 了 handleDataChange 事件 reload", dataPath);
			configManage.reload();
		}

		public void handleDataDeleted(String dataPath) throws Exception {
			logger.info("触发了 handleDataDeleted 事件 reload", dataPath);
			configManage.reload();
		}

		public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
			logger.info("触发了 handleChildChange 事件 reload", parentPath);
			configManage.reload();
		}
	}
}
